<template>
  <div class="app-container" style="height:100%">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-card class="box-card" shadow="never" body-style="width:100%;height:350px;overflow:auto">
            <el-tree :props="defaultProps" :data="treeOptions" default-expand-all :expand-on-click-node="false"
                     ref="tree" highlight-current @node-click="handleNodeClick"/>
          </el-card>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <!-- 列表 -->
        <el-table v-loading="loading"
                  :data="list"
                  border
                  height="350"
                  @selection-change="selectionChange">
          <el-table-column
            type="selection"
            fixed
            width="45"/>
          <el-table-column label="编码" align="center" prop="code"/>
          <el-table-column label="名称" align="center" prop="name"/>
          <el-table-column label="描述" align="center" prop="description"/>
        </el-table>
        <!-- 分页组件 -->
        <pagination :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>

import {
  getRolePage, getMyRoleFolderList
} from "@/api/sa/role";

import {COMMON_TREE, FOLDER_USER_AUTH_TYPE, OrgKindEnum} from "@/utils/constants";
import {getCommonTreeList} from "@/api/sa/commonTreeBiz";

export default {
  name: "SaRolePicker",
  props: {
    // 值
    // value: [String, Object, Array]
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      defaultProps: {
        children: "children",
        label: "name"
      },
      treeOptions: [],
      currentCommonTree: {},
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 角色 列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
      }

    };
  },
  watch: {
    value: {
      handler(val) {
        if (val) {

        } else {

        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    handleNodeClick(data) {
      this.currentCommonTree = data
      this.queryParams.folderId = data.id
      this.getList()

    },
    async getTreeList() {
      // 查询有权限的角色目录
      let folderDataRes = await getMyRoleFolderList()
      let folderIdMap = {}
      folderDataRes.data.forEach(id => {
        folderIdMap[id] = 1
      })
      getCommonTreeList({kindId: COMMON_TREE.ROLE.id}).then(response => {
        this.treeOptions = []
        let root = {id: COMMON_TREE.ROOT.id}
        let data = response.data ? response.data : []
        let treeData = []
        data.forEach(row => {
          if (row.parentId == root.id || folderIdMap[row.id] === 1) {
            treeData.push(row);
          }
        })
        treeData.push(root)
        data = this.handleTree(treeData, "id", undefined, undefined, root.id)
        this.treeOptions.push(...data);

      });
    },
    // tree懒加载
    nodeExpand(node, resolve) {
      let id = node.level == 0 ? COMMON_TREE.ROOT.id : node.data.id
      getCommonTreeList({parentId: id, kindId: COMMON_TREE.ROLE.id}).then(response => {
        resolve(response.data);
        if (node.level == 0) {
          this.queryParams.folderId = response.data[0].id;
        }
        this.loading = false;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getRolePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    selectionChange(data) {
      this.$emit('select-change', data)
    }
  }
}
</script>

<style scoped>
.container {
  height: 500px

}

.card-height {
  height: 400px;
  overflow-y: scroll
}

.div-opr {
  text-align: center;
  padding-top: 130px;
  height: 300px
}


</style>
